package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.im.domain.dao.mapper.UserAuthTokenMapper;
import com.liveinstars.im.domain.dao.model.UserAuthToken;
import com.liveinstars.im.domain.manager.UserAuthTokenManager;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户鉴权表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-31
 */
@Service
public class UserAuthTokenManagerImpl extends ServiceImpl<UserAuthTokenMapper, UserAuthToken> implements UserAuthTokenManager {

    private static final Logger logger = LoggerFactory.getLogger(UserAuthTokenManagerImpl.class);

    @Override
    public UserAuthToken getByUserIdAndDeviceIdAndAppCode(Long userId, String deviceId, String appCode) {
        try {
            QueryWrapper<UserAuthToken> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("device_id", deviceId);
            queryWrapper.eq("app_code", appCode);
            List<UserAuthToken> list = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
            return null;
        } catch (Exception e) {
            logger.error("getByUserIdAndDeviceIdAndAppCode userId:{} deviceId:{} app_code:{} errorMsg:{}", userId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void sava(UserAuthToken userAuthToken) {
        try {

        } catch (Exception e) {

        }
    }

    @Override
    public void updateSelectiveById(UserAuthToken userAuthToken) {

    }
}
